import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

import '../../common/utils/utils.dart';
import '../../common/values/values.dart';
import '../../common/widgets/widgets.dart';

class SignInPage extends StatefulWidget {
  const SignInPage({super.key});

  @override
  State<SignInPage> createState() => _SignInPageState();
}

class _SignInPageState extends State<SignInPage> {
  final TextEditingController emailController = TextEditingController();
  final TextEditingController pwdController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: Center(
        child: Column(
          children: [
            buildLogo(),
            buildForm(),
            Spacer(),
            buildThirdPartyLogin(),
            buildSignupButton(),
          ],
        ),
      ),
    );
  }

  Widget buildLogo() {
    return Container(
      width: 110,
      margin: EdgeInsets.only(top: 80.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Container(
            width: 76,
            height: 76,
            // color: Colors.red,
            margin: EdgeInsets.symmetric(horizontal: 15),
            child: Stack(
              alignment: Alignment.center,
              children: [
                Positioned(
                  left: 0,
                  top: 0,
                  right: 0,
                  child: Container(
                    height: 76,
                    decoration: BoxDecoration(
                      color: Colors.white,
                      boxShadow: [
                        BoxShadow(
                          blurRadius: 2,
                          offset: Offset(2, 2),
                          color: Color.fromARGB(38, 27, 27, 29),
                        ),
                      ],
                      borderRadius: BorderRadius.all(Radius.circular(76 * 0.5)),
                    ),
                    child: Container(),
                  ),
                ),
                Positioned(
                  top: 10,
                  child: Image.asset(
                    'assets/images/favicon.ico',
                    width: 56,
                    height: 56,
                    fit: BoxFit.fill,
                  ),
                )
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.only(top: 15),
            child: Text(
              '腾牛个性网',
              textAlign: TextAlign.center,
              style: TextStyle(
                color: AppColors.primaryText,
                fontFamily: 'Montserrat',
                fontWeight: FontWeight.w600,
                fontSize: 21,
                height: 1,
              ),
            ),
          ),
          Text(
            'news',
            textAlign: TextAlign.center,
            style: TextStyle(
                color: AppColors.primaryText,
                fontFamily: 'Avenir',
                fontWeight: FontWeight.w400,
                fontSize: 16,
                height: 1),
          ),
        ],
      ),
    );
  }

  Widget buildForm() {
    return Container(
      width: 295,
      margin: EdgeInsets.only(top: 49),
      child: Column(
        children: [
          inputTextEdit(
            controller: emailController,
            keyboardType: TextInputType.emailAddress,
            hintText: '邮箱',
            marginTop: 0,
          ),
          inputTextEdit(
            controller: pwdController,
            hintText: '密码',
            isPwd: true,
          ),
          Container(
            height: 44,
            margin: EdgeInsets.only(top: 15),
            child: Row(
              children: [
                buttonWidget(
                  onPressed: () {
                    Navigator.pushNamed(context, '/sign-up');
                  },
                  title: '注册',
                  bgColor: AppColors.thirdElement,
                ),
                Spacer(),
                buttonWidget(
                  onPressed: () async {
                    if (!RegExp(
                            "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*\$")
                        .hasMatch(emailController.value.text)) {
                      toast(msg: '邮箱格式不正确！');
                    } else if (pwdController.value.text.length < 6) {
                      toast(msg: '密码不能少于6位！');
                    } else {
                      final dio = Dio();
                      Map req = {'username': emailController.value.text, 'password': duSHA256(pwdController.value.text)};
                      print('登陆接口请求：$req');
                      Response res = await dio.post('http://127.0.0.1:3000/user/info', data: req);
                      print('登陆接口响应：$res');
                      if (res.data is Map<String, dynamic>) {
                        Map<String, dynamic> jsonData = res.data;
                        jsonData.forEach((key, value) {
                          print('Key: $key, Value: $value');
                        });
                      }
                      Navigator.pushNamed(context, '/index');
                    }
                  },
                  title: '登录',
                  color: AppColors.primaryElementText,
                  bgColor: AppColors.primaryElement,
                ),
              ],
            ),
          ),
          Container(
            height: 36,
            margin: EdgeInsets.only(top: 10),
            child: TextButton(
              onPressed: () {
                toast(msg: '怎么办呀，怎么办？');
              },
              child: Text(
                '忘记密码?',
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontFamily: 'Avenir',
                  fontSize: 16,
                  height: 1,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget iconBtn(imgUrl) {
    return Container(
      width: 40,
      height: 40,
      child: TextButton(
        child: Image.network(imgUrl, fit: BoxFit.fill),
        onPressed: null,
        style: ButtonStyle(
          padding: MaterialStateProperty.all(EdgeInsets.zero),
        ),
      ),
    );
  }

  Widget buildThirdPartyLogin() {
    return Container(
      width: 295,
      margin: EdgeInsets.only(bottom: 40),
      child: Column(
        children: [
          Text(
            '更多登录方式',
            textAlign: TextAlign.center,
            style: TextStyle(
              color: AppColors.primaryText,
              fontFamily: 'Avenir',
              fontSize: 16,
            ),
          ),
          Padding(
            padding: EdgeInsets.only(top: 20),
            child: Row(
              children: [
                iconBtn('https://p.qqan.com/up/2020-10/202010201023217399.jpg'),
                Spacer(),
                iconBtn('https://p.qqan.com/up/2018-1/15153707713590269.jpg'),
                Spacer(),
                iconBtn('https://p.qqan.com/up/2018-1/15171883375133024.jpg'),
                Spacer(),
                iconBtn('https://p.qqan.com/up/2018-4/15232573744499937.jpg'),
                Spacer(),
                iconBtn('https://p.qqan.com/up/2021-10/20211025933548393.jpg'),
                Spacer(),
                iconBtn('https://p.qqan.com/up/2018-8/2018082410241924441.jpg'),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget buildSignupButton() {
    return Container(
      margin: EdgeInsets.only(bottom: 38),
      child: buttonWidget(
        onPressed: () {
          Navigator.pushNamed(context, '/sign-up');
        },
        title: '注册',
        color: AppColors.primaryText,
        bgColor: AppColors.secondaryElement,
      ),
    );
  }
}
